package bb.lanxing.common.engin.util;

import android.location.Location;
import android.os.Build;

import java.io.FileWriter;
import java.io.IOException;

import bb.lanxing.calc.data.GpsPoint;
import bb.lanxing.model.database.Trackpoint;

public class LocationRecorder {
    private static LocationRecorder instance;
    private boolean isRecordOpen;
    private FileWriter mWriter;

    private LocationRecorder() {
    }

    public static LocationRecorder get() {
        if (instance == null) {
            instance = new LocationRecorder();
        }
        return instance;
    }

    public boolean initRecord(boolean z, boolean z2) {
        this.isRecordOpen = z;
        if (z) {
            try {
                this.mWriter = new FileWriter(EnginUtil.getGpsSimulateFile(), z2);
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public boolean recordLocation(GpsPoint gpsPoint) {
        FileWriter fileWriter;
        if (!this.isRecordOpen || (fileWriter = this.mWriter) == null) {
            return false;
        }
        try {
            fileWriter.write(encode(gpsPoint));
            this.mWriter.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }

    public boolean recordLocation(Location location) {
        FileWriter fileWriter;
        if (!this.isRecordOpen || (fileWriter = this.mWriter) == null) {
            return false;
        }
        try {
            fileWriter.write(encode(location));
            this.mWriter.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }

    public boolean recordLocation(Trackpoint trackpoint) {
        FileWriter fileWriter;
        if (!this.isRecordOpen || (fileWriter = this.mWriter) == null) {
            return false;
        }
        try {
            fileWriter.write(encode(trackpoint));
            this.mWriter.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return true;
        }
    }

    public boolean releaseRecord() {
        this.isRecordOpen = false;
        FileWriter fileWriter = this.mWriter;
        if (fileWriter != null) {
            try {
                fileWriter.flush();
                this.mWriter.close();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                this.mWriter = null;
            }
        }
        return false;
    }

    private String encode(GpsPoint gpsPoint) {
        return encode(gpsPoint.getTimestamp(), gpsPoint.getLatitude(), gpsPoint.getLongitude(), gpsPoint.getAltitude(), gpsPoint.getAccuracy(), gpsPoint.getSpeed(), gpsPoint.getBearing(), "GpsPoint", String.valueOf(gpsPoint.getTimestamp() * 1000));
    }

    private String encode(Trackpoint trackpoint) {
        return encode(trackpoint.getTime(), trackpoint.getLatitude(), trackpoint.getLongitude(), trackpoint.getAltitude(), trackpoint.getAccuracy(), trackpoint.getSpeed(), trackpoint.getBearing(), "Workout", String.valueOf(trackpoint.getTime() * 1000));
    }

    private String encode(Location location) {
        return encode(location.getTime(), location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getAccuracy(), location.getSpeed(), location.getBearing(), location.getProvider(), Build.VERSION.SDK_INT >= 17 ? String.valueOf(location.getElapsedRealtimeNanos()) : "");
    }

    private String encode(long j, double d, double d2, double d3, double d4, double d5, double d6, String str, String str2) {
        return j + "," + d + "," + d2 + "," + d3 + "," + d4 + "," + d5 + "," + d6 + "," + str + "," + str2 + "\n";
    }
}